var vapp = new Vue({
	el: "#app",
	data: {
		dicts:[],
		code:'',
		date:[],
		websock: null,
	},
	mounted: function(){
		this.initData();
	},
	created: function(){
		//this.initWebSocket();
	},
	destroyed: function(){
		// this.websock.close();
	},
	methods: {
		
		 initWebSocket:function(){ //初始化weosocket
		        const wsuri = "ws://127.0.0.1:8080/ssm/websocket";
		        this.websock = new WebSocket(wsuri);
		        this.websock.onmessage = this.websocketonmessage;
		        this.websock.onopen = this.websocketonopen;
		        this.websock.onerror = this.websocketonerror;
		        this.websock.onclose = this.websocketclose;
		      },
		  websocketonopen:function(){ //连接建立之后执行send方法发送数据
		        let actions = {"test":"12345"};
		        this.websocketsend(JSON.stringify(actions));
		      },
		  websocketonerror:function(){//连接建立失败重连
		        this.initWebSocket();
		      },
		  websocketonmessage:function(e){ //数据接收
		        const redata = JSON.parse(e.data);
		        console.log(redata);
		      },
		  websocketsend:function(data){//数据发送
		        this.websock.send(data);
		      },
		  websocketclose:function(e){  //关闭
		        console.log('断开连接',e);
		  },
		
		
		initData:function(){
			this.dicts = data_gather;
			this.date.push(util.addDays(new Date(),-7));
			this.date.push(new Date());
		},
		save:function(){
			var that =this;
			if(this.code==''){
				this.showError("请选择测试地址");
				return false;
			}
			var url = '';
			this.dicts.forEach(function(item,idx){
				if(that.code==item.code){
					url = item.remarks;
					name = item.code_desc+"_"+util.formatTime(new Date(),'yyyyMMddHHmmss');
				}
			});
			
			var beginDate = this.date!=null?util.formatTime(this.date[0],'yyyy-MM-dd'):"";
	        var endDate = this.date!=null?util.formatTime(this.date[1],'yyyy-MM-dd'):"";
	        var para2 = util.daysBetween(this.date[0],this.date[1]);
	        var jsonStr = {};
	        if(this.code=='2'){
				jsonStr = {
					"name":name,
					"url":url,
					"req_data":"para="+beginDate+"&para2="+para2,
				};
			}else{
				jsonStr = {
					"name":name,
					"url":url,
					"req_data": "start_date="+beginDate+"&end_date="+endDate,
				};
			}
			var reqData = {
				"jsonStr":JSON.stringify(jsonStr)
			};
			var data = tools.ajaxRequest("../schedule/scheduleSave", reqData);
			if(data.respCode=="0"){
				this.$message({
					  showClose: true,
			          message: '保存成功',
			          type: 'success'
			    });
				console.log(data);
			}else{
				this.showError(data.respDesc);
				return false;
			}

		},
		
		
		showError: function(info){
			var i = (info==undefined?"加载数据错误":info);
			this.$alert(i, '系统错误', {
		          confirmButtonText: '确定'
		    });	
		}
		
	},
	
});